﻿using Bilibili.Model.Resource;
using Bilibili.Model.Video;
using Newtonsoft.Json.Linq;
using PCLUntils.Objects;
using System;
using System.Threading.Tasks;

namespace Bilibili.WebClient.Api
{
    public sealed class InteractiveClient : ClientBase
    {
        private string Id { get; }
        private int GraphVersion { get; }
        public InteractiveClient(string aid, int graphVersion)
        {
            Id = aid;
            GraphVersion = graphVersion;
        }
        public InteractiveClient(string aid, VideoInfoModels info)
        {
            Id = aid;
            var graphVersion = info.Interaction == null ? 467 : info.Interaction.Graph_Version;
            GraphVersion = graphVersion;
        }
        public async Task<ReturnModel<NodeInfo>> InteractionEdgeInfo(int nodeid = 0)
        {
            try
            {
                ApiInfo apiInfo = new ApiInfo("https://api.bilibili.com/x/stein/nodeinfo", $"&aid={Id}&delay=0&graph_version={GraphVersion}&node_id={nodeid}", true);
                var results = await ApiClient.GetResults(apiInfo);
                var obj = JObject.Parse(results);
                if (obj["code"].ToInt32() == 0)
                {
                    var model = obj["data"].ToObject<NodeInfo>();
                    return BuildSuccessResult(model);
                }
                else
                {
                    return BuildFailedResult<NodeInfo>();
                }
            }
            catch (Exception ex)
            {
                return BuildExceptionResult<NodeInfo>(ex);
            }
        }
    }
}